package com.hlhj.funlike_business.utils

import android.content.Context
import android.graphics.Bitmap
import android.os.Environment
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by Never Fear   on 2018\9\27 0027.
Never More....
 */
object FileUtils {
    /*创建图片*/
    @Throws(IOException::class)
    fun createImageFile(b: Bitmap, context: Context): File {
        // Create an image file name
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(Date())
        val imageFileName = "JPEG_" + timeStamp + "_"
        //.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录，一般放一些长时间保存的数据
        val storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        //创建临时文件,文件前缀不能少于三个字符,后缀如果为空默认未".tmp"
        val image = File.createTempFile(
                imageFileName, /* 前缀 */
                ".jpg", /* 后缀 */
                storageDir      /* 文件夹 */
        )
        val mCurrentPhotoPath = image.absolutePath
        var fOut: FileOutputStream? = null
        try {
            fOut = FileOutputStream(image)
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        }

        b.compress(Bitmap.CompressFormat.PNG, 100, fOut)
        try {
            fOut!!.flush()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        try {
            fOut!!.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return image
    }



}